<?php

namespace Admin\Controller;

use Common\Lib\Tree;

class PurviewController extends AdminController {

    public function index() {
        $result = M('PurviewModule')->order('sort ASC,id ASC')->select();
        foreach ($result as $key => $val) {
            if ($val['pid'] == 0) {
                $result[$key]['add'] = '<a href="' . U('purview/add', ['pid' => $val['id']]) . '" class=" ajax-load  btn btn-xs  "><i class="fa fa-plus"></i>添加子模块</a>';
            } else {
                $actions = M('Purview')->where(['mid' => $val['id']])->order('id ASC')->getField('id,name', true);
                if (!empty($actions)) {
                    foreach ($actions as $aid => $aname) {
                        $result[$key]['actions'] .= '<div class="btn-group margin-right-10">
	                                        <a class="btn btn-sm btn-info">' . $aname . '</a>
	                                        <a class="btn btn-sm btn-info dropdown-toggle" data-toggle="dropdown" aria-expanded="false"><i class="fa fa-angle-down"></i></a>
	                                        <ul class="dropdown-menu dropdown-info" role="menu">
	                                            <li>
	                                                <a href="' . U('purview/action_edit', ['id' => $aid]) . '" class="ajax-load">编辑</a>
	                                            </li>
                                                      <li>
	                                                <a href="' . U('purview/action_delete', ['id' => $aid]) . '" data-confirm="确定删除？">删除</a>
	                                            </li>
	                                        </ul>
	                                    </div>';
                    }
                } else {
                    $result[$key]['actions'] = '';
                }

                $result[$key]['delete_link'] = U('purview/delete', ['id' => $val['id']]);
                $result[$key]['edit_link'] = U('purview/edit', ['id' => $val['id']]);
                $result[$key]['add'] = '<a href="' . U('purview/action_add', ['mid' => $val['id']]) . '" class=" ajax-load btn btn-xs"><i class="fa fa-plus"></i>添加操作</a>';
            }
        }

        $Tree = new Tree($result);
        $Tree->icon = array("&nbsp;&nbsp;&nbsp;&nbsp; ", "&nbsp;&nbsp;&nbsp;├─ ", "&nbsp;&nbsp;&nbsp;└─ ");
        $Tree->nbsp = "&nbsp;&nbsp;&nbsp;";
        $html = "<tr>
                    <td width='80' class='input-group-xs'><input type='text' name='sort[\$id]' style='text-align:center' value='\$sort' class='form-control' /></td>
                    <td class='left'><span class='spacer'>\$spacer</span>\$name </td>
                    <td>\$actions</td>
                    <td>\$add <a href='\$edit_link' class='ajax-load btn btn-xs margin-left-10 '><i class='fa fa-edit'></i> 编辑</a> <a href='\$delete_link'  class='margin-left-10 ajax-link  btn btn-xs ' data-confirm='确定删除？'><i class='fa fa-trash-o'></i> 删除</a></td>
            </tr>";
        $result = $Tree->get_tree(0, $html);
        $this->assign('result', $result);
        $this->display();
    }

    public function add() {
        if (IS_POST) {
            $data = I('post.');
            $data['menus'] = array2string($data['menus']);
            if (!$data['name']) {
                $this->error('请填写模块名称');
            }
            if (M('PurviewModule')->data($data)->add()) {
                $this->success('添加成功');
            } else {
                $this->error('添加失败');
            }
        } else {
            $pid = I('pid', 0, 'intval');
            $parents = M('PurviewModule')->where(['pid' => 0])->order('sort ASC,id ASC')->getField('id,name', true);
            $this->assign('treeMenu', A('Menu')->treeMenu());
            $this->assign('pid', $pid);
            $this->assign('parents', $parents);
            $this->display();
        }
    }

    public function edit() {
        if (IS_POST) {
            $data = I('post.');
            $data['menus'] = array2string($data['menus']);
            if (!$data['name']) {
                $this->error('请填写模块名称');
            }
            if (false !== M('PurviewModule')->data($data)->save()) {
                $this->success('修改成功');
            } else {
                $this->error('修改失败');
            }
        } else {
            $id = I('id', 0, 'intval');
            $result = M('PurviewModule')->find($id);
            $parents = M('PurviewModule')->where(['pid' => 0])->order('sort ASC,id ASC')->getField('id,name', true);
            $menus = string2array($result['menus']);
            $this->assign('treeMenu', A('Menu')->treeMenu($menus));
            $this->assign($result);
            $this->assign('parents', $parents);
            $this->display();
        }
    }

    public function delete($id) {
        M('PurviewModule')->delete($id);
        $this->success('删除成功');
    }

    public function sort() {
        $sorts = I('sort', 0, 'intval');
        foreach ($sorts as $key => $val) {
            M('PurviewModule')->where(['id' => $key])->setField('sort', $val);
        }
        $this->success('操作成功');
    }

    public function action_add() {
        if (IS_POST) {
            $data = I('post.');

            $data['purview'] = array2string($data['purview']);

            if (M('Purview')->data($data)->add()) {
                $this->success('添加成功');
            } else {
                $this->error('添加失败');
            }
        } else {
            $modules = M('PurviewModule')->order('sort ASC,pid ASC,id DESC')->select();
            $this->assign('modules', tree_array($modules));
            $this->assign('actions', $this->treeAction());
            $this->assign('mid', I('mid', 0, 'intval'));
            $this->display();
        }
    }

    public function action_edit() {
        if (IS_POST) {
            $data = I('post.');
            $data['purview'] = array2string($data['purview']);
            if (M('Purview')->data($data)->save() !== false) {
                $this->success('编辑成功');
            } else {
                $this->error('编辑失败');
            }
        } else {
            $id = I('id', 0, 'intval');
            $result = M('Purview')->find($id);
            $modules = M('PurviewModule')->order('sort ASC,pid ASC,id DESC')->select();
            $this->assign('modules', tree_array($modules));
            $this->assign('actions', $this->treeAction());
            $result['purview'] = string2array($result['purview']);
            $this->assign($result);
            $this->display();
        }
    }

    public function action_delete($id) {
        M('Purview')->delete($id);
        $this->success('删除成功');
    }

    public function treeAction() {
        $NO_AUTHEN = C('NO_AUTHEN');
        $modulePath = MODULE_PATH . 'Controller';
        $actions = array();
        foreach (glob($modulePath . '/*Controller.class.php') as $file) {
            preg_match('/' . addcslashes($modulePath, './') . '\/(.*)Controller\.class\.php/', $file, $fileName);
            $className = $fileName[1];
            if ($className != 'Admin') {
                $refClass = new \ReflectionClass(A($className));
                if (!$refClass->isAbstract()) {
                    foreach ($refClass->getMethods(256) as $method) {
                        $methodName = $method->getName();
                        if ($methodName != '__construct') {
                            if (preg_match('/^[a-z][a-z_]+$/', $methodName)) {
                                if($NO_AUTHEN){ 
                                    foreach($NO_AUTHEN as $val){ 
                                        list($passController,$passAction) = explode('/', $val);
                                        if(strtolower($passController) == strtolower($className) && ($passAction == '*' || strtolower($passAction) == strtolower($methodName))){ 
                                            continue 2;                                            
                                        }
                                    }                                    
                                }
                                $actions[$className][] = $methodName;
                            }
                        } else {
                            break;
                        }
                    }
                }
            }
        }
        return $actions;
    }

}
